<?php
/**
 * Laravel-Vue-Admin
 * @Author:技术铺
 * @Date:2021/11/22
 */

namespace App\Services;

use App\Http\Resources\DictionaryCollection;
use App\Models\Dictionary;

/**
 * 数据字典服务层 : 提供各模块所需枚举选项等服务
 *
 * Class DictionaryService
 * @package App\Services
 */
class DictionaryService
{
    /**
     * 获取所有字典列表
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable|mixed
     */
    public static function getAllList()
    {
        $result = CacheService::getCache('dictionaryService');
        if ($result == null) {
            $request = request();
            $resource = Dictionary::where('status', 1)->get();
            $result = DictionaryCollection::make($resource)->only(['id', 'name', 'id_name', 'dictionaryItems' => ['id', 'name', 'value', 'description']])->toArray($request);
            CacheService::setCache('dictionaryService', $result);
        }
        return $result ? $result['items'] : [];
    }

    /**
     * 获取指定字典信息
     * @param string $id_name 英文标识
     * @return array|mixed
     */
    public static function getDetail($id_name = "")
    {
        $getAllList = self::getAllList();
        if ($id_name == '' || !$getAllList) return [];
        $result = collect($getAllList)->first(function ($value, $key) use ($id_name) {
            return $value['id_name'] == $id_name;
        });
        return $result ?: [];
    }

    /**
     * 获取字典名称
     * @param string $id_name
     * @return mixed|string
     */
    public static function getName($id_name = "")
    {
        $getDetail = self::getDetail($id_name);
        return $getDetail ? $getDetail['name'] : '';
    }

    /**
     * 获取字典的选项列表
     * @param string $id_name
     * @return array|mixed
     */
    public static function getItems($id_name = "")
    {
        $getDetail = self::getDetail($id_name);
        return $getDetail ? $getDetail['dictionaryItems'] : [];
    }

    /**
     * 获取字典选项列表中的指定项
     * @param string $id_name
     * @param string $dictionaryItems_value
     * @return array|mixed
     */
    public static function getOneItem($id_name = "", $dictionaryItems_value = "")
    {
        $getItems = self::getItems($id_name);
        if (!$getItems) return [];
        $result = collect($getItems)->first(function ($value, $key) use ($dictionaryItems_value) {
            return $value['value'] == $dictionaryItems_value;
        });
        return $result ?: [];
    }

    /**
     * 获取字典选项名称
     * @param string $id_name
     * @param string $dictionaryItems_value
     * @return mixed|string
     */
    public static function getItemName($id_name = "", $dictionaryItems_value = "")
    {
        $getOneItem = self::getOneItem($id_name, $dictionaryItems_value);
        return $getOneItem ? $getOneItem['name'] : '';
    }

    /**
     * 获取字典选项描述
     * @param string $id_name
     * @param string $dictionaryItems_value
     * @return mixed|string
     */
    public static function getItemDescription($id_name = "", $dictionaryItems_value = "")
    {
        $getOneItem = self::getOneItem($id_name, $dictionaryItems_value);
        return $getOneItem ? $getOneItem['description'] : '';
    }

    /**
     * 获取选项名称数组集合
     * @param string $id_name
     * @return array|\Illuminate\Support\Collection
     */
    public static function getItemsNamesArray($id_name = "")
    {
        $getItems = self::getItems($id_name);
        if (!$getItems) return [];
        $result = collect($getItems)->map(function ($item) {
            return $item['name'];
        })->toArray();
        return $result ?: [];
    }

    /**
     * 获取选项值数组集合
     * @param string $id_name
     * @return array|\Illuminate\Support\Collection
     */
    public static function getItemsValuesArray($id_name = "")
    {
        $getItems = self::getItems($id_name);
        if (!$getItems) return [];
        $result = collect($getItems)->map(function ($item) {
            return $item['value'];
        })->toArray();
        return $result ?: [];
    }

    /**
     * 获取选项值字符串集合
     * @param string $id_name
     * @param string $join 拼接方式，默认逗号拼接
     * @return string
     */
    public static function getItemsValuesString($id_name = "", $join = ",")
    {
        $getItemsValuesArray = self::getItemsValuesArray($id_name);
        return $getItemsValuesArray ? join($join, $getItemsValuesArray) : '';
    }

    /**
     * 判断选项值是否存在选项值列表中
     * @param string $id_name
     * @param string $value
     * @return bool
     */
    public static function valueExist($id_name = "", $value = "")
    {
        $getItemsValuesArray = self::getItemsValuesArray($id_name);
        if (!$getItemsValuesArray) return false;
        return in_array($value, $getItemsValuesArray);
    }


    /**
     * 获取选项描述数组集合
     * @param string $id_name
     * @return array|\Illuminate\Support\Collection
     */
    public static function getItemsDescriptionsArray($id_name = "")
    {
        $getItems = self::getItems($id_name);
        if (!$getItems) return [];
        $result = collect($getItems)->map(function ($item) {
            return $item['description'];
        })->toArray();
        return $result ?: [];
    }

    /**
     * 判断描述是否存在选项描述列表中
     * @param string $id_name
     * @param string $description
     * @return bool
     */
    public static function descriptionExist($id_name = "", $description = "")
    {
        $getItemsDescriptionsArray = self::getItemsDescriptionsArray($id_name);
        if (!$getItemsDescriptionsArray) return false;
        return in_array($description, $getItemsDescriptionsArray);
    }

    /**
     * 按照描述分组获取选项值数组集合
     * 场景：根据描述值“image”从附件类型字典中获取图片附件列表
     * @param string $id_name
     * @param string $description
     * @return array
     */
    public static function getItemsValuesArrayGroupByDesc($id_name = "", $description = "")
    {
        $getItems = self::getItems($id_name);
        if (!$getItems || !$description) return [];
        $result = collect($getItems)->where('description', $description)->map(function ($item) use ($description) {
            if ($description == $item['description']) {
                return $item['value'];
            }
        })->filter()->toArray();
        return $result ?: [];
    }
}
